﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

namespace HomeDMSDomain
{
    public partial class BindnFindObjectsDataContext
    {
        public void CreateAssociation(Document doc, Tag tag)
        {
            var q = from dt in Document_Tags where dt.document_id == doc.document_id && tag.tag_id == dt.tag_id select dt;
            if (q.Count<Document_Tag>() == 0)
            {
                Document_Tag assoc = new Document_Tag { document_id = doc.document_id, tag_id = tag.tag_id };
                Document_Tags.InsertOnSubmit(assoc);
            }
        }

        public static string CurrentDB { get; set; }

        public static BindnFindObjectsDataContext Instance
        {
            get
            {
                if (string.IsNullOrEmpty(CurrentDB))
                {
                    throw new ArgumentException("No data base associate");
                }

                return new BindnFindObjectsDataContext(string.Format("Data Source={0};Max Database Size=4091;Persist Security Info=False;", CurrentDB));
            }
        }

        public void PrefillDatabaseWithData()
        {
            Tags.InsertAllOnSubmit(new Tag[] 
            {
                new Tag {Name="receipt"},
                new Tag {Name="check"},
                new Tag {Name="paycheck"},
                new Tag {Name="invoice"},
                new Tag {Name="docket"},
                new Tag {Name="fine"},
                new Tag {Name="letter"},
                new Tag {Name="photo"},
                new Tag {Name="picture"},
                new Tag {Name="will"},
                new Tag {Name="note"},
                new Tag {Name="book"},
                new Tag {Name="bill"},
                new Tag {Name="lecture"},
                new Tag {Name="contract"},
                new Tag {Name="insurance"},
                new Tag {Name="card"},
                new Tag {Name="recepie"},
                new Tag {Name="mobile"},
                new Tag {Name="phone"},
                new Tag {Name="electricity"},
                new Tag {Name="municipality"},
                new Tag {Name="tax"},
                new Tag {Name="tv"},
                new Tag {Name="cable"},
                new Tag {Name="water"},
                new Tag {Name="sewer"},
                new Tag {Name="medical"},
                new Tag {Name="bank"},
                new Tag {Name="gas"},
                new Tag {Name="creditcard"},
                new Tag {Name="internet"}
            });
            this.SubmitChanges();
        }
    }


    public partial class Document
    {
        public string Keywords
        {
            get
            {
                var q = from t in this.Document_Tags select t.Tag.Name;
                StringBuilder sb = new StringBuilder();
                int counter = 0;
                foreach (string item in q)
                {
                    if (counter == 0)
                    {
                        sb.Append(item);
                    }
                    else
                    {
                        sb.AppendFormat(", {0}", item);
                    }
                    counter++;
                }
                return sb.ToString();
            }
        }
    }

    public partial class Tag
    {
        public override string ToString()
        {
            return this.Name;
        }
    }
}
